<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="起因" prop="reason">
        <el-input v-model="dataForm.reason" placeholder="起因"></el-input>
      </el-form-item>
      <el-form-item label="结果" prop="result">
        <el-input v-model="dataForm.result" placeholder="结果"></el-input>
      </el-form-item>
      <el-form-item label="发生时间" prop="createtime">
        <el-date-picker
          v-model="dataForm.createtime"
          align="right"
          type="date"
          placeholder="选择日期"
          :picker-options="pickerOptions"
        ></el-date-picker>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() > Date.now();
        }
      },
      visible: false,
      dataForm: {
        eventId: 0,
        reason: "",
        result: "",
        state: "",
        createtime: "",
        updatetime: ""
      },
      dataRule: {
        reason: [{ required: true, message: "起因不能为空", trigger: "blur" }],
        result: [{ required: true, message: "结果不能为空", trigger: "blur" }],
        state: [{ required: true, message: "状态不能为空", trigger: "blur" }],
        createtime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
        updatetime: [
          { required: true, message: "修改时间不能为空", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    init(id) {
      this.dataForm.eventId = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.eventId) {
          this.$http({
            url: this.$http.adornUrl(
              `/manager/event/info/${this.dataForm.eventId}`
            ),
            method: "get",
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.reason = data.event.reason;
              this.dataForm.result = data.event.result;
              this.dataForm.state = data.event.state;
              this.dataForm.createtime = data.event.createtime;
              this.dataForm.updatetime = data.event.updatetime;
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/manager/event/${!this.dataForm.eventId ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              eventId: this.dataForm.eventId || undefined,
              reason: this.dataForm.reason,
              result: this.dataForm.result,
              state: this.dataForm.state,
              createtime: this.dataForm.createtime,
              updatetime: this.dataForm.updatetime
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                }
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    }
  }
};
</script>
